package com.back.service.impl;

import com.back.dto.CompanyRecruiterDTO;
import com.back.entity.CompanyRecruiter;
import com.back.entity.User;
import com.back.mapper.CompanyRecruiterMapper;
import com.back.mapper.UserMapper;
import com.back.service.ICompanyRecruiterService;
import com.back.vo.CompanyRecruiterVO;
import com.back.vo.UserVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  企业招聘人员服务实现类
 * </p>
 *
 * @author liyang
 * @since 2025-04-26
 */
@Service
public class CompanyRecruiterServiceImpl extends ServiceImpl<CompanyRecruiterMapper, CompanyRecruiter> implements ICompanyRecruiterService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public IPage<CompanyRecruiterVO> getRecruiterList(Page<CompanyRecruiter> page, Long companyId) {
        // 构建查询条件
        LambdaQueryWrapper<CompanyRecruiter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CompanyRecruiter::getCompanyId, companyId)
                    .eq(CompanyRecruiter::getDeleted, false)
                    .orderByDesc(CompanyRecruiter::getIsAdmin)
                    .orderByDesc(CompanyRecruiter::getCreateTime);
        
        // 执行分页查询
        Page<CompanyRecruiter> recruiterPage = page(page, queryWrapper);
        
        // 转换为VO
        Page<CompanyRecruiterVO> resultPage = new Page<>();
        BeanUtils.copyProperties(recruiterPage, resultPage, "records");
        
        // 转换记录，并关联用户信息
        List<CompanyRecruiterVO> recruiterVOList = recruiterPage.getRecords().stream().map(recruiter -> {
            CompanyRecruiterVO recruiterVO = new CompanyRecruiterVO();
            BeanUtils.copyProperties(recruiter, recruiterVO);
            
            // 关联用户信息
            User user = userMapper.selectById(recruiter.getUserId());
            if (user != null) {
                UserVO userVO = new UserVO();
                BeanUtils.copyProperties(user, userVO);
                recruiterVO.setUser(userVO);
            }
            
            return recruiterVO;
        }).collect(Collectors.toList());
        
        resultPage.setRecords(recruiterVOList);
        
        return resultPage;
    }

    @Override
    public CompanyRecruiterVO addRecruiter(CompanyRecruiterDTO recruiterDTO) {
        // 查询用户是否存在
        User user = userMapper.selectByEmail(recruiterDTO.getEmail());
        if (user == null) {
            return null;
        }
        
        // 检查是否已经是该企业的招聘人员
        LambdaQueryWrapper<CompanyRecruiter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CompanyRecruiter::getCompanyId, recruiterDTO.getCompanyId())
                    .eq(CompanyRecruiter::getUserId, user.getId())
                    .eq(CompanyRecruiter::getDeleted, false);
        
        if (count(queryWrapper) > 0) {
            return null; // 已经是招聘人员
        }
        
        // 创建招聘人员
        CompanyRecruiter recruiter = new CompanyRecruiter();
        BeanUtils.copyProperties(recruiterDTO, recruiter);
        recruiter.setUserId(user.getId());
        
        // 设置默认值
        if (recruiter.getIsAdmin() == null) {
            recruiter.setIsAdmin(false);
        }
        
        // 设置时间
        long currentTime = System.currentTimeMillis();
        recruiter.setCreateTime(currentTime);
        recruiter.setUpdateTime(currentTime);
        recruiter.setDeleted(false);
        
        // 保存到数据库
        save(recruiter);
        
        // 转换为VO返回
        CompanyRecruiterVO recruiterVO = new CompanyRecruiterVO();
        BeanUtils.copyProperties(recruiter, recruiterVO);
        
        // 设置用户信息
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        recruiterVO.setUser(userVO);
        
        return recruiterVO;
    }

    @Override
    public CompanyRecruiterVO updateRecruiter(Long companyId, Long id, CompanyRecruiterDTO recruiterDTO) {
        // 查询招聘人员
        LambdaQueryWrapper<CompanyRecruiter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CompanyRecruiter::getId, id)
                    .eq(CompanyRecruiter::getCompanyId, companyId)
                    .eq(CompanyRecruiter::getDeleted, false);
        
        CompanyRecruiter recruiter = getOne(queryWrapper);
        if (recruiter == null) {
            return null;
        }
        
        // 更新属性
        BeanUtils.copyProperties(recruiterDTO, recruiter, "companyId", "email", "userId");
        recruiter.setUpdateTime(System.currentTimeMillis());
        
        // 保存到数据库
        updateById(recruiter);
        
        // 转换为VO返回
        CompanyRecruiterVO recruiterVO = new CompanyRecruiterVO();
        BeanUtils.copyProperties(recruiter, recruiterVO);
        
        // 设置用户信息
        User user = userMapper.selectById(recruiter.getUserId());
        if (user != null) {
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(user, userVO);
            recruiterVO.setUser(userVO);
        }
        
        return recruiterVO;
    }

    @Override
    public boolean deleteRecruiter(Long companyId, Long id) {
        // 查询招聘人员
        LambdaQueryWrapper<CompanyRecruiter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CompanyRecruiter::getId, id)
                    .eq(CompanyRecruiter::getCompanyId, companyId)
                    .eq(CompanyRecruiter::getDeleted, false);
        
        CompanyRecruiter recruiter = getOne(queryWrapper);
        if (recruiter == null) {
            return false;
        }
        
        // 逻辑删除
        recruiter.setDeleted(true);
        recruiter.setUpdateTime(System.currentTimeMillis());
        
        return updateById(recruiter);
    }

    @Override
    public boolean isCompanyAdmin(Long companyId, Long userId) {
        // 构建查询条件
        LambdaQueryWrapper<CompanyRecruiter> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CompanyRecruiter::getCompanyId, companyId)
                    .eq(CompanyRecruiter::getUserId, userId)
                    .eq(CompanyRecruiter::getIsAdmin, true)
                    .eq(CompanyRecruiter::getDeleted, false);
        
        // 查询是否存在
        return count(queryWrapper) > 0;
    }
}
